package com.qcby.controller;

import com.qcby.common.Result;
import com.qcby.entity.TProduct;
import com.qcby.form.TProductForm;
import com.qcby.form.TProductListForm;
import com.qcby.service.TCategoryService;
import com.qcby.service.TProductService;
import com.qcby.vo.PageVO;
import com.qcby.vo.TProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2025-02-04
 */
@RestController
@RequestMapping("/product")
public class TProductController {
    @Autowired
    private TProductService productService;
    @Autowired
    private TCategoryService categoryService;
    /**
     * 商品列表
     */
    @GetMapping("/list")
    public Result<PageVO> list(@Valid TProductListForm form) {
        PageVO pageVO = productService.productList(form);
        return Result.success(pageVO);
    }
    /**
     * 商品详情
     */
    @GetMapping("/{id}")
    public Result<TProductVO> detail(@PathVariable("id") Integer id) {
        TProductVO detail = productService.getProductDetail(id);
        return Result.success(detail);
    }
    /**
     * 商品编辑
     */
    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Integer id){
        if(id == 0) {
            Map<String, List> map = new HashMap<>();
            map.put("categoryList", this.categoryService.list());
            return Result.ok().put("data",map);
        } else {
            Map<String,Object> map = new HashMap<>();
            map.put("product", this.productService.getById(id));
            map.put("categoryList", this.categoryService.list());
            return Result.ok().put("data", map);
        }
    }

    @PutMapping("/edit")
    public Result edit(@RequestBody TProduct product){
        boolean updateById = this.productService.updateById(product);
        if(!updateById) return Result.error("商品修改失败");
        return Result.ok();
    }
    /**
     * 添加商品
     */
    @PostMapping
    public Result add(@RequestBody @Valid TProductForm form) {
        productService.addProduct(form);
        return Result.success();
    }
    /**
     * 修改商品
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Integer id, @RequestBody @Valid TProductForm form) {
        productService.updateProduct(id, form);
        return Result.success();
    }
    /**
     * 删除商品
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        productService.deleteProduct(id);
        return Result.success();
    }
    /**
     * 商品上架
     */
    @PutMapping("/{id}/on-shelf")
    public Result onShelf(@PathVariable("id") Integer id) {
        productService.onShelf(id);
        return Result.success();
    }
    /**
     * 商品下架
     */
    @PutMapping("/{id}/off-shelf")
    public Result offShelf(@PathVariable("id") Integer id) {
        productService.offShelf(id);
        return Result.success();
    }
}

